
const BaseEntity = require('../../../libs/base/BaseEntity');
const Code = require('../../../config/code');
const sysconfig = require('../../../config/sysconfig');
const FoodManager = require('../food/FoodManager');
class PlayerEntity extends BaseEntity {
    constructor() {
        super();
        this.entityName = 'Player';
        this.pid = '';
        this.openid = '';
        this.platform = '';
        this.unionid = '';
        this.nickName = '';
        this.fullName = '';
        this.passwordHash = '';
        this.avatarUrl = '';
        this.gender = '0';
        this.language = '';
        this.realName = '';
        this.idCardNumber = '';
        this.phoneNumber = '';
        this.email = 0;
        this.dateOfBirth = '';
        this.country = '';
        this.province = '';
        this.city = '';
        this.addressLine1 = '';
        this.addressLine2 = '';
        this.isActive = '';
        this.isVerified = '';
        this.lastLoginAt = '';
        this.createdAt = '';
        //记录玩家各种业务信息
        this.work = {};
    }
    async init(info) {
        let result = await Redis.get(`${this.entityName}:${info.pid}`);
        if (result) {
            this.setInfo(result.data);
        } else {
            result = await MySQL.loginByPid(info.pid);
            if (result.code == Code.SUCCESS) {
                await Redis.set(`${this.entityName}:${info.pid}`, result.data);
                this.setInfo(result);
            } else {
                return result;
            }
        }
    }
    setInfo(info) {
        for (let key in info) {
            this[key] = info[key];
        }
    }
    workInit(name) {
        switch (name) {
            case sysconfig.playerWork.FOOD:
                this.work[name] = {
                    taboo: "",
                    foodlist: [[], [], []],
                    location: { "latitude": 0, "longitude": 0 },
                    info: "",
                    working: new FoodManager(this)
                }
            default:
                return Code.HALL_PLAYER_WORK_FAIL;
        }
    }
    doWork(workName, method, ...args) {
        if (!this.work[workName] || !this.work[workName].working[method]) {
            return Code.HALL_PLAYER_WORK_FAIL;
        }
        if (!!this.work[workName].working[method]) {
            return this.work[workName].working[method](...args);
        } else {
            return Code.HALL_PLAYER_WORK_FAIL;
        }
    }
    reset() {
        this.pid = '';
        this.openid = '';
        this.platform = '';
        this.unionid = '';
        this.nickName = '';
        this.fullName = '';
        this.passwordHash = '';
        this.avatarUrl = '';
        this.gender = '0';
        this.language = '';
        this.realName = '';
        this.idCardNumber = '';
        this.phoneNumber = '';
        this.email = 0;
        this.dateOfBirth = '';
        this.country = '';
        this.province = '';
        this.city = '';
        this.addressLine1 = '';
        this.addressLine2 = '';
        this.isActive = '';
        this.isVerified = '';
        this.lastLoginAt = new Date().getTime();
        this.createdAt = '';
    }
}
module.exports = PlayerEntity;